package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.memberrecharge;

import android.app.Activity;
import android.content.Context;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.base.BaseViewMode;
import com.zzsy.carosshopprojects.bean.ActivityBean;
import com.zzsy.carosshopprojects.bean.MainCardListBean;
import com.zzsy.carosshopprojects.bean.MemberInfoBean;
import com.zzsy.carosshopprojects.bean.MemberResultBean;
import com.zzsy.carosshopprojects.bean.ResterPostionBeans;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.lifecycle.MutableLiveData;

public class MemberRechargeMode extends BaseViewMode {
    private MutableLiveData<List<ResterPostionBeans>> liveData;
    private MutableLiveData<List<ActivityBean>> liveDataAb;
    private MutableLiveData<MemberResultBean> liveMrbData;

    public MutableLiveData<List<ResterPostionBeans>> getLiveData() {
        if (liveData == null)
            liveData = new MutableLiveData<>();
        return liveData;
    }

    public MutableLiveData<List<ActivityBean>> getLiveAbData() {
        if (liveDataAb == null)
            liveDataAb = new MutableLiveData<>();
        return liveDataAb;
    }

    public MutableLiveData<MemberResultBean> getLiveMrbData() {
        if (liveMrbData == null)
            liveMrbData = new MutableLiveData<>();
        return liveMrbData;
    }

    /**
     * 获取充值档位
     */
    public void getOilsRegsterData() {
        List<ResterPostionBeans> selectRegsterMoneyList = new ArrayList<>();

        if (DataUtil.getInstance().getStoreId().equals("56") || DataUtil.getInstance().getStoreId().equals("57")) {
            ResterPostionBeans regsterPositionNewBean0 = new ResterPostionBeans();
            regsterPositionNewBean0.setAmountStr("500");
            regsterPositionNewBean0.setAmount(50000);
            selectRegsterMoneyList.add(regsterPositionNewBean0);
        }

        ResterPostionBeans regsterPositionNewBean1 = new ResterPostionBeans();
        regsterPositionNewBean1.setAmountStr("1000");
        regsterPositionNewBean1.setAmount(100000);

        ResterPostionBeans regsterPositionNewBean2 = new ResterPostionBeans();
        regsterPositionNewBean2.setAmountStr("2000");
        regsterPositionNewBean2.setAmount(200000);

        ResterPostionBeans regsterPositionNewBean3 = new ResterPostionBeans();
        regsterPositionNewBean3.setAmountStr("3000");
        regsterPositionNewBean3.setAmount(300000);

        ResterPostionBeans regsterPositionNewBean4 = new ResterPostionBeans();
        regsterPositionNewBean4.setAmountStr("5000");
        regsterPositionNewBean4.setAmount(500000);
        selectRegsterMoneyList.add(regsterPositionNewBean1);
        selectRegsterMoneyList.add(regsterPositionNewBean2);
        selectRegsterMoneyList.add(regsterPositionNewBean3);
        selectRegsterMoneyList.add(regsterPositionNewBean4);
        liveData.postValue(selectRegsterMoneyList);
    }

    /**
     * 根据输入充值金额匹配充值档位
     */
    public void getCoumoserList(Context context, String amount, String memberId, String payType, String productId, String orderSource, String productInfoId) {
        Map<String, String> map = new HashMap<>();
        map.put("activityType", "2");
        map.put("amount", amount);
        map.put("memberId", memberId);
        map.put("payType", payType);
        map.put("productId", productId);
        map.put("storeId", DataUtil.getInstance().getStoreId());
        map.put("orderSource", orderSource);
        map.put("productInfoId", productInfoId);
        RequestUtils.RechargeActivity(context, map, new MyObserver<List<ActivityBean>>(context) {
            @Override
            public void onSuccess(List<ActivityBean> result) {
                liveDataAb.postValue(result);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context, errorMsg);
            }
        });
    }

    //通过UUID 获取会员信息
    public void getUUIDMemeberData(Activity context, String uuid) {
        RequestUtils.MemberByUuid(context, uuid, DataUtil.getInstance().getCompanyId(), new MyObserver<MemberInfoBean>(context) {
            @Override
            public void onSuccess(MemberInfoBean result) {
                if (result != null) {
                    String parentId = result.getParentId();
                    if (!StringUtils.isEmpty(parentId) && !"0".equals(parentId)) {
                        ToastUtils.showToast(context, "副卡暂不能充值！");
                        context.finish();
                        return;
                    }

                    String memberId = result.getMemberId();
                    //会员姓名
                    String realName = result.getRealName();
                    if (StringUtils.isEmpty(realName)) {
                        realName = "暂未设置姓名";
                    }

                    //会员电话号
                    String phone = result.getPhone();
                    if (StringUtils.isEmpty(phone)) {
                        phone = "";
                    }
                    //会员卡id
                    String memberCardId = result.getMemberCardId();
                    if (StringUtils.isEmpty(memberCardId)) {
                        memberCardId = "";
                    }

                    //充值余额
                    String AcountBalance = result.getBalance();

                    String productId = result.getProductId();

                    String productName = result.getProductName();

                    if (StringUtils.isEmpty(memberId)) {
                        ToastUtils.showToast(context, "此卡暂未开通会员！");
                        context.finish();
                    } else {
                        MemberResultBean memberResultBean = new MemberResultBean();
                        memberResultBean.setParentId(parentId);
                        memberResultBean.setRealName(realName);
                        memberResultBean.setMemberId(memberId);
                        memberResultBean.setMemberCardId(memberCardId);
                        memberResultBean.setRechargeBalance(AcountBalance);
                        memberResultBean.setProductId(productId);
                        memberResultBean.setProductName(productName);
                        memberResultBean.setPhone(phone);
                        liveMrbData.postValue(memberResultBean);
                    }

                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context, errorMsg);
                context.finish();
            }
        });
    }

    /**
     * 根据手机号查询会员信息
     */
    public void getPhoneMemberDta(Activity context, MainCardListBean.MyMainCardsBean cardBean) {
        String memberId = cardBean.getMemberId();
        //会员姓名
        String memberName = cardBean.getRealName();
        //会员电话号
        String phone = cardBean.getPhone();
        if (StringUtils.isEmpty(phone)) {
            phone = "";
        }
        //会员卡id
        String memberCardId = cardBean.getId();
        if (StringUtils.isEmpty(memberCardId)) {
            memberCardId = "";
        }
        if (StringUtils.isEmpty(memberName)) {
            memberName = "暂未设置姓名";
        }
        //充值余额
        String AcountBalance = cardBean.getRechargeBalance();

        String productId = cardBean.getProductId();
        String productName = cardBean.getProductName();

        if (StringUtils.isEmpty(memberId)) {
            ToastUtils.showToast(context, "此卡暂未开通会员！");
            context.finish();
        } else {
            MemberResultBean memberResultBean = new MemberResultBean();
            memberResultBean.setRealName(memberName);
            memberResultBean.setMemberId(memberId);
            memberResultBean.setMemberCardId(memberCardId);
            memberResultBean.setRechargeBalance(AcountBalance);
            memberResultBean.setProductId(productId);
            memberResultBean.setProductName(productName);
            memberResultBean.setPhone(phone);
            liveMrbData.postValue(memberResultBean);
        }
    }
}
